import { defineStore } from 'pinia'
import { store } from '@/store'
import { userLoginApi, userInfoApi } from "@/api";

export const useUserStore = defineStore('user-store', {
    state: () => {
        return {
            token: uni.getStorageSync('token') || null,
            userInfo: uni.getStorageSync('userInfo') || {},
        }
    },
    actions: {
        async userLogin(apiForm) {
            const { token, userInfo } = await userLoginApi(apiForm);
            this.token = token
            this.userInfo = userInfo
            uni.setStorageSync('token', token)
            uni.setStorageSync('userInfo', userInfo)
            // await userInfoApi()
        },
        async getUserInfo() {
            const userInfo = await userInfoApi()
            this.userInfo = userInfo
            uni.setStorageSync('userInfo', userInfo)
        },
        userLoginOut() {
            uni.removeStorageSync('token')
            uni.removeStorageSync('userInfo')
            this.token = null
            this.userInfo = {}
        }
    },
})

export function useUserStoreWithOut() {
    return useUserStore(store)
}
